[LegacyColorValue = true]; 

Vars:ExpDate(1201231);
{
Copyright (c) 1996 Stuart Okorofsky
}

{***This indicator will only plot the MA if there is an X-Day
      High or Low***}

Inputs:Proj(0),Hi_Prc(High),Low_Prc(Low),MA_Type(1),MALen(3),Smooth(3),
           LookBack(12),   {This is the  "X" value in the x bar high or low}
           Bars2Plt(4),  {Number of bars to plot}
Hi_Fact(1),Low_Fact(1);

Vars:HighPrice(0),LowPrice(0),HighAvg(0),LowAvg(0),
       Factor(0),HiXAverageVal(0),LowXAverageVal(0),x(0),
       PlotOffset(0),Ok2PlotHigh(false),Ok2PlotLow(false),
       HighPlotBar(0),LowPlotBar(0),MoveLine(0); 
 
{

MA_Type
1 - Simple
2 - Smooth
3 - EXP
4 - Weighted
5 - Centered
}

if date < ExpDate and CurrentDate <  ExpDate then BEGIN

if Barnumber = 1 and Proj > 0 then

begin  {**Need to set up for projection of lines to the right***}
     MoveLine = IntPortion(.5+Proj*MALen);    
end;

if barnumber = 1 and MA_Type = 5 then 
               PlotOffset = IntPortion(MALen/2+1);

HighPrice = HI_Prc;
LowPrice = Low_Prc;

{
if Barnumber >= MALen + 1 then
}
begin
     if MA_Type = 1 or MA_Type = 5 then 
     begin 
          HighAvg = Average(HighPrice,MALen);
          LowAvg  = Average(LowPrice,MALen);
     end;
     if MA_Type = 2 then 
     begin
          HighAvg = Average(Average(HighPrice,MALen),Smooth);
          LowAvg  = Average(Average(LowPrice,MALen),Smooth);
     end;
     if MA_Type = 3 then
     begin  {***Calculate EXP Average here***}
          If MALen + 1 <> 0 then 
          begin
               If CurrentBar = MALen + 1 then 
               begin
                    Factor = 2 / (MALen + 1);
                    HiXAverageVal = HighPrice;
                    LowXAverageVal = LowPrice;
                    HighAvg = HighPrice;
                    LowAvg = LowPrice;
               end
           Else  
               begin         
                     HiXAverageVal = Factor * HighPrice + (1 - Factor) * HiXAverageVal[1];
                     LowXAverageVal = Factor * LowPrice + (1 - Factor) * LowXAverageVal[1];              
               end;
           end;
           HighAvg = HiXAverageVal;
           LowAvg  = LowXAverageVal;
     end;
     if MA_Type = 4 then
     begin 
          HighAvg = WAverage(HighPrice,MALen);
          LowAvg  = WAverage(LowPrice,MALen);
     end;
 end;


{****ADJUST PRICES IF NECESSARY****}
HighAvg = HighAvg*Hi_Fact;
LowAvg = LowAvg*Low_Fact;

If high < lowest(HighPrice,LookBack)[1] then 
begin  {New High, set up for plot}
     OK2PlotHigh=true;
     HighPlotBar = barnumber;    
end;

if OK2PlotHigh then
begin
      if barnumber >= HighPlotBar + Bars2Plt 
          then OK2PlotHigh = false;
     if OK2PlotHigh and MoveLine = 0 and Barnumber > MaLen
           then Plot1[PlotOffset](HighAvg,"High-MA");
end;

If low > Highest(LowPrice,LookBack)[1] then
begin   {***New Low, set up for plot***}
     OK2PlotLow=true;
     LowPlotBar = barnumber;
end;

if OK2PlotLow then
begin      
      if barnumber >= LowPlotBar + Bars2Plt 
          then OK2PlotLow = false;
      if OK2PlotLow and MoveLine=0 and Barnumber > MaLen
           then Plot2[PlotOffset](LowAvg,"Low-MA");
end;

if OK2PlotHigh[MoveLine]  and Barnumber > MaLen then
   Plot1[PlotOffset](HighAvg[MoveLine],"High-MA");

if OK2PlotLow[MoveLine] and Barnumber > MaLen then
    Plot2[PlotOffset](LowAvg[MoveLine],"Low-MA");
   

end;



